package com.njmaomao.gj.pyot.domain.wechat.wechatapi;

import com.njmaomao.gj.pyot.config.CertRestTemplateFactory;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.orderquery.OrderQueryReq;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.orderquery.OrderQueryResp;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.refund.RefundReq;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.refund.RefundResp;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.refundquery.RefundQueryReq;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.refundquery.RefundQueryResp;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.sandboxnew.GetSignkeyReq;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.sandboxnew.GetSignkeyResp;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.unifiedorder.UnifiedorderReq;
import com.njmaomao.gj.pyot.domain.wechat.wechatapi.unifiedorder.UnifiedorderResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component
public class WechatApiService extends ApiService {


    @Autowired
    private WechatApiUrlConfigFactory wechatApiUrlConfigFactory;

    @Resource(name = "wechatRestTemplate")
    private RestTemplate restTemplate;

    @Autowired
    private CertRestTemplateFactory certRestTemplateFactory;

    public UnifiedorderResp unifiedorder(boolean isSandbox, UnifiedorderReq unifiedorderReq) {

        return post(wechatApiUrlConfigFactory.get(isSandbox).getUnifiedorder(), unifiedorderReq, UnifiedorderResp.class);
    }

    public OrderQueryResp orderquery(boolean isSandbox, OrderQueryReq orderQueryReq) {
        return post(wechatApiUrlConfigFactory.get(isSandbox).getOrderquery(), orderQueryReq, OrderQueryResp.class);
    }

    public RefundResp refund(boolean isSandbox, RefundReq refundReq) {
        RestTemplate restTemplate = certRestTemplateFactory.get(refundReq.getAppid());
        return post(wechatApiUrlConfigFactory.get(isSandbox).getRefund(), refundReq, RefundResp.class, restTemplate);
    }

    public RefundQueryResp refundquery(boolean isSandbox, RefundQueryReq refundQueryReq) {
        return post(wechatApiUrlConfigFactory.get(isSandbox).getRefundquery(), refundQueryReq, RefundQueryResp.class);
    }

    public GetSignkeyResp getsandboxsignkey(GetSignkeyReq getSignkeyReq) {
        return post(wechatApiUrlConfigFactory.getGetsandboxsignkey(), getSignkeyReq, GetSignkeyResp.class);
    }

    @Override
    protected RestTemplate getRestTemplate() {
        return restTemplate;
    }

}
